package com.ly.hotel.cloud;

import java.util.Arrays;
import java.util.LinkedHashMap;

import android.os.Build;
import android.util.Log;

import com.google.gson.Gson;
import com.ly.hotel.adaptor.HardwareProperty;
import com.ly.hotel.common.Constants;
import com.ly.hotel.common.MyApplication;
import com.ly.hotel.common.SharedPreferencesConfig;
import com.ly.hotel.model.ChatQrCodeInfo;
import com.ly.hotel.msgevent.SendLogEvent;

import de.greenrobot.event.EventBus;
/*提供给云app的接口，必须返回String ,没有返回值的，一律返回null
 * 参数必须的Object... objs，然后在代码里面分割成String
 * 函数的参数是用","号作分割的*/
public class FunctionRpc {

	MyApplication app = (MyApplication) MyApplication.getContext();
	
	public FunctionRpc() {
		
    } 
	
	public String getInterfaceVersion(Object... objs) {
		return MyApplication.interfaceVersion;
	}
	
	/*获取授权的会所名*/
	public String getAuthInfo(Object... objs){
		return app.getAuthToCompany();
	}
	
	public String putMySpace(Object... objs/*String key, String value*/){
		String param[] = Arrays.toString(objs).replace("[", "").replace("]", "").split(",");
		String key = param[0];
		String value = param[1];
		
		Log.i("MySpace", "putMySpace:" + key + "<-" + value);
		app.mySpacePutValue(key, value);
		
		return null;
	}
	
	public String getMySpace(Object... objs/*final String key*/){
		String param[] = Arrays.toString(objs).replace("[", "").replace("]", "").split(",");
		String key = param[0];
		
		Log.i("MySpace", "getMySpace:" + key + "->" + app.mySpaceGetValue(key));
		return app.mySpaceGetValue(key);
	}	
	
	public String delMySpace(Object... objs/*String key*/){
		String param[] = Arrays.toString(objs).replace("[", "").replace("]", "").split(",");
		String key = param[0];		
		Log.i("MySpace", "delMySpace:" + key);
		app.mySpaceDelKey(key);
		
		return null;
	}  
	
	public String getKeysMySpace(Object... objs) {
		Log.i("MySpace", "getKeysMySpace:" + app.mySpaceGenKeys());
		return app.mySpaceGenKeys();
	}
	
	public String containKeysMySpace(Object... objs/*String key*/) {
		String param[] = Arrays.toString(objs).replace("[", "").replace("]", "").split(",");
		String key = param[0];		
		Log.i("MySpace", "containKeysMySpace:" + app.mySpaceIsKey(key));
		return app.mySpaceIsKey(key);
	}
	
	public String emptyMySpace(Object... objs) {
		Log.i("MySpace", "emptyMySpace");
		app.mySpaceEmpty();
		
		return null;
	}
	
	//获取支持的电影类型
	public String getSupportVodFormat(Object... objs){
		String v[] = HardwareProperty.getSupportVodFormat();
		String r = "";
		
		for(int i = 0;i<v.length;i++){
			r += v[i];
			r += ",";
		}
		
		return r.substring(0,r.length()-1);
	}
	
	//获取支持的最大视频大小
	public String getSupportVodSize(Object... objs){

		return String.valueOf(HardwareProperty.getSupportVodSize());
	}

	//获取是否支持电影
	public String getMovieEnable(Object... objs){

		return SharedPreferencesConfig.getMovieEnable(MyApplication.getContext()) + "";
	}

	/*获取机器MAC*/
	public String getMAC(Object... objs){
		return MyApplication.getMAC();
	}
	
	/*获取机器的区域*/
	public String getCategoryName(Object... objs){
		return MyApplication.getDeviceInfo().getCategoryName();
	}
	
	/*获取机器的位置号*/
	public String getDeviceName(Object... objs){
		return MyApplication.getDeviceInfo().getName();
	}
	
	/*专门打印调试信息用的,使用adb ddms可以看到*/
	public String myDebug(Object... objs/*String key*/){
		String param[] = Arrays.toString(objs).replace("[", "").replace("]", "").split(",");
		String key = param[0];		
		Log.i("webappdebug", key);
		
		return null;
	}  
	
	/*获取机器的位置号*/
	public String getDeviceType(Object... objs){
		if(Build.BOARD.equals(Constants.BOARD_TYPE_ADA628_NAME)){
			return "ADA628";
		} else if(Build.BOARD.equals(Constants.BOARD_TYPE_ADA801_NAME)){
			return "ADA801";
		} else if(Build.BOARD.equals(Constants.BOARD_TYPE_ADA648_NAME)){
			return "ADA648";
		}else if(Build.BOARD.equals(Constants.BOARD_TYPE_6A648_NAME)){
			return "6A648";
		}else{
			return "BOX4.4";
		}
	}
	
	public String getDeviceLanguage(Object... objs){
		return MyApplication.currectLocale.toString();
	}
	
	public String sendLog(Object... objs/*String msg*/) {
		String param[] = Arrays.toString(objs).replace("[", "").replace("]", "").split(",");
		String msg = param[0];
//		Log.i("webappdebug", "日志->" + msg);
		EventBus.getDefault().post(new SendLogEvent(msg));
		
		return null;
	}
	
	public String showChatQrCode(Object... objs/*String key*/){
		boolean rv = false;
		String param[] = Arrays.toString(objs).replace("[", "").replace("]", "").replace("%20", "").replace(" ", "").split(",");
		String url = param[0];	//要转成二维码的连接
		String num = param[1];	//技师号
		String tips = param[2];	//技师留言
		
		Log.i("rpc", url + ";" + num + ";" + tips);
		if(!MyApplication.chatQrCodeDisturb){
			ChatQrCodeInfo info = new ChatQrCodeInfo();
			info.url = url;
			info.tips = tips;
			info.num = num;
			MyApplication.chatQrCodeInfo = info;
			rv = true;
		}
		return formatString2Json("showChatQrCode",rv + "");//rv + "";
	}  	
	
	//获取是否允许技师聊天推送
	public String getChatQrCodeEnable(Object... objs){

		return formatString2Json("getChatQrCodeEnable",MyApplication.chatQrCodeDisturb + "");//;
	}
	
	private String formatString2Json(String fuc, String data){
		LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
        map.put("cmd","tvcontent");
        map.put("mac",MyApplication.getMAC());
        map.put("fuc",fuc);
        map.put("data",data);
        Gson gson = new Gson();
        
        String json = gson.toJson(map);
		
		return json;
	}
}
